package com.ld.admin.controller.system;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.admin.controller.AdminBasicController;
import com.ld.common.plugin.PluginManager;
import com.ld.common.util.task.TaskManager;
import com.ld.shieldsb.common.core.model.PropertiesModel;
import com.ld.shieldsb.common.core.util.StringUtils;
import com.ld.shieldsb.common.web.util.Web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 
 * 应用设置管理
 * 
 * @ClassName ApplicationSetController
 * @author 吕凯
 * @date 2019年02月19日 11:02:27
 *
 */
@Api(tags = "应用设置管理")
@ConditionalOnProperty(prefix = "modules.sc.system", name = "applicationset.enable", havingValue = "true", matchIfMissing = true)
@Controller(value = "systemApplicationSetController")
@RequestMapping("${shieldsb.adminPath}/system/applicationset")
public class ApplicationSetController extends AdminBasicController<Object> {

    @ApiOperation(value = "到配置页面", notes = "")
    @RequestMapping(path = "", method = RequestMethod.GET)
    public String config() {
        request.setAttribute("taskSet", TaskManager.getPkgFilterMap());
        request.setAttribute("pluginSet", PluginManager.getPkgFilterMap());
        return "a/system/applicationset/applicationset_list";
    }

    @ApiOperation(value = "刷新配置信息", notes = "")
    @RequestMapping(path = "/refresh", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object refresh() throws ServletException, IOException {
        String type = getString("ty");
        if ("task".equals(type)) {
            TaskManager.getPkgNamesSet();
        } else if ("plugin".equals(type)) {
            PluginManager.getPkgNamesSet();
        }
        return Web.Response.success("刷新配置成功！");
    }

    @ApiOperation(value = "跳转到更新页面", notes = "")
    @RequestMapping(path = "/toUpdate", method = RequestMethod.GET)
    public String config2oUpdate() {
        setAttributes();
        String type = getString("ty");
        Map<String, String> map = new HashMap<>();
        if ("task".equals(type)) {
            map = TaskManager.getPkgFilterMap();
        } else if ("plugin".equals(type)) {
            map = PluginManager.getPkgFilterMap();
        }
        request.setAttribute("map", map);
        return "a/system/applicationset/applicationset_update";
    }

    @ApiOperation(value = "更新配置信息", notes = "")
    @RequestMapping(path = "/update", method = RequestMethod.POST)
    // 返回json格式
    @ResponseBody
    public Object configUpdate() throws ServletException, IOException {
        String scanpkg = getString("value1");
        scanpkg = getStrSplitNewline(scanpkg);
        String pkgfilter = getString("value2");
        pkgfilter = getStrSplitNewline(pkgfilter);
        String type = getString("ty");
        if (StringUtils.isNotBlank(type)) {
            if ("task".equals(type)) {
                TaskManager.setPkgNamesSet(scanpkg, pkgfilter);
                TaskManager.getPkgNamesSet(); // 刷新配置
            } else if ("plugin".equals(type)) {
                PluginManager.setPkgNamesSet(scanpkg, pkgfilter);
                PluginManager.getPkgNamesSet(); // 刷新配置
            }

        }
        return Web.Response.success("更新配置成功！");
    }

    /**
     * 根据换行符拆分字符并拼接为新的字符
     * 
     * @Title getStrSplitNewline
     * @author 吕凯
     * @date 2019年3月6日 下午2:54:26
     * @param value1
     * @return String
     */
    private String getStrSplitNewline(String value1) {
        if (value1 == null) {
            return null;
        }
        String[] value1Arr = value1.split("\r\n|\n"); // window下回车是由\r\n(即0x0D和0x0A)组成的，注意不是\n\r，而linux下回车是由\n（即0x0A）
        String value1Str = StringUtils.join(value1Arr, PropertiesModel.PROJECT.getListDelimiter());
        log.warn(value1Str + " " + value1Arr.length);
        return value1Str;
    }

}